#include "murder.h"
#include "Strat.hpp"
#include "idle.h"

#include <iostream>

Murder::Murder(Fruit *idP, int id){
    type = T_MURDER;
    idParent = idP;
    idTarget = id;
    x=0;
    y=0;
    value=0;
    age=0;
}


bool Murder::execute() {

    //cout << "Je vais faire un meurte!";
    Fruit* target;
    {
        map<int,Fruit>::iterator needle = ai.world.theirFruits.find(idTarget);

        if( needle == ai.world.theirFruits.end() ){
            delete idParent->task;
            idParent->task = new Idle();
            return false;
        }

        target = &(needle->second);
    }
    value = idParent->attack(idTarget);
    switch( value ){
        case HIT:{
            return true;
        }
        case SPLATCHED:{
            delete idParent->task;
            idParent->task = new Idle();
            return true;
        }
        default:{
            path.clear();
            pf.start = idParent->position;
            pf.end = target->position;
            if(pf.compute(&path,0,0)==false) return false; //On fonce comme des brutes donc on need pas tout ça :D
            //cout << "Numpt" << path.size();
            {
                list<map_chunk*>::iterator it = path.begin();
                for( int i = 0; i < idParent->speed; i++ ) if( it != path.end() )it++;
                if( it == path.end() ) it--;
                if( (*it) == target->position ) it--;

                printf("Speed %d Origine (%d,%d), Destination (%d,%d)\n",idParent->speed
                       ,idParent->position->X_coord
                       ,idParent->position->Y_coord
                       ,(*it)->X_coord
                       ,(*it)->Y_coord);

                switch (idParent->move((*it)->X_coord, (*it)->Y_coord ) ){
                    case UNKNOWN_OBJECT: cout << "unknown object"; break;
                    case NOT_FRUIT: cout << "not fruit"; break;
                    case NOT_OWNER: cout << "not owner"; break;
                    case NOT_VALID_DESTINATION: cout << "destination invalide"; break;
                    case NO_MORE_ACTIONS: cout << "no more actions"; break;
                    case NOT_VALID_POSITION : cout << "position invalide"; break;
                    case TOO_FAR: cout << "Trop loin"; break;
                    case OK: cout << "OK"; break;

                }
            }
            return true;
        }

        return false;
    }
}

